// 引入接口文件
import {regionArea,locationServ,projectsServ} from './expandCustomerMapServ.js'

// 对象和JSON字符串互转
import { parse } from 'qs'
// 弹出消息框
import { message } from 'antd'
// 日期处理对象
import moment from 'moment'
/* isEmpty 对象是否为空
 * isArray 对象是否数组
 * cloneDeep 对象深拷贝
 */
import {isEmpty, isArray, cloneDeep, merge} from 'lodash'
// 引入路由对象
import { hashHistory } from 'dva/router'
import { makeAddress } from "utils/util"

const queryList = [
  {
    name: '区域', type: 'select', key: 'region', value: '', notNeedAll: true, list: []
  },
  {
    name: '项目', type: 'select', key: 'productId', value: '', notNeedAll: true, list:[]
  },
  {
    name: '地图展现形式', type: 'select', key: 'mapType', value: '1', notNeedAll: true, list: [
      {id: '1', name: '打点图'},
      {id: '2', name: '热力图'}
    ]
  },
  {
    name: '客户状态', type: 'selectClear', key: 'customerstatus', value: '', notNeedAll: true, list: [
      {id: '1', name: '已报备'},
      {id: '2', name: '已到访'},
      {id: '3', name: '已成交'}
    ]
  },
  {
    name: '意向房型', type: 'selectClear', key: 'type', value: '', notNeedAll: true, list: [
      {id: '1', name: '二室'},
      {id: '2', name: '三室'},
      {id: '3', name: '四室'}
    ]
  },
  {
    name: '意向面积', type: 'selectClear', key: 'area', value: '', notNeedAll: true, list: [
      {id: '1', name: '90m²以下'},
      {id: '2', name: '90-100m²'},
      {id: '3', name: '120m²以上'}
    ]
  },
]

// config props 热力图配置
const visible = true; 
const radius = 30; 
const gradient = {
  0.5: 'blue',
  0.65: 'rgb(117,211,248)',
  0.7: 'rgb(0, 255, 0)',
  0.9: '#ffea00',
  1.0: 'red'
};
const zooms = [3, 18]; 

const pluginProps = {
  visible,
  radius,
  gradient,
  zooms,
  dataSet:{
    data:[],
    max:100
  }
}

// 初始默认状态
const defultState = {
  loading: false,
  btnLoading: false,
  biz: {
		queryForm: {}
  },
  queryList,
  products:[],
  areaList:[],
  area1:[],
  center:{},
  markersGroup:[],
  mapType:'1',
  pluginProps//热力图配置
}



// 声明module
const tmpModule = {
  namespace: 'messageListModel',
  state: cloneDeep(defultState),
  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(({ pathname, query }) => {
        if('' + tmpModule.pathname === '' + pathname){
          dispatch({ type: 'regionArea', payload:{}});
          dispatch({ type: 'updateStore', payload: cloneDeep(defultState) });
        }
      })
    },
  },
  // 方法列表
  effects: {
    *regionArea({ payload },{ put, call, select }){
      let {biz,areaList,queryList} = yield select(state => state[tmpModule.namespace])
      const { data, success } = yield call(regionArea);
      if(success) {
        data.map((item,index)=>{
          item.id = item.value
          item.name = item.label
        })
        queryList[0].list =data
        yield put({ type: 'updateStore',payload:{area1:data,areaList,queryList}})
      }
    },
    
  //查询操作
  *search({ payload = {} }, { call, put, select }) {
    let {queryList} = yield select(state => state[tmpModule.namespace])
    //组装接口入参
    let params = {
    };
    queryList.map(item => {
      console.log(item)
      if(item.value===''||item.value==undefined){
        //当查询选项数据为空时，把全部选项的值遍历到查询中去
        if(item.list){
          var idArr = []
          let newStr = ''
          item.list.map((item,index)=>{
            idArr=[...idArr,item.id]
            newStr = idArr.join(',')
          })
          params[item.key] = newStr
        }
        return false
      }else {
        params[item.key] = item.value;
      }
    })
    const { success,rows} = yield call(locationServ, {...params})
    yield put({type: 'updateStore', payload: {loading: true, btnLoading: true}})
    if(success){
      let status = {loading:false,btnLoading:false,mapType:params.mapType}
      let {arrived=[],traded=[],reported=[]} = rows
      let newRows = [...arrived,...traded,...reported]
      newRows.map((item,index)=>{
        item.longitude = item.lng
        item.latitude = item.lat
        item.countNum?item.count = item.countNum:''
      })
      if(params.mapType=='2'){
        
        
        // newRows=newRows.map((item)=>{
        //   delete item.longitude
        //   delete item.latitude
        //   delete item.countNum
        //   delete item.productId
        //   return item=mapKeys(item,function(value, key){
        //     return  `"${key}"`
        //   })
        // })
        pluginProps.dataSet.data = newRows
        status = {...status,pluginProps}
      }else{
        status = {...status,markersGroup:newRows}
      }

      yield put({type: 'updateStore', payload: status}) 

    }
  },
  // 查询条件重置
  *queryReset({ }, { call, put, select }) {
    let { area1} = yield select(state => state[tmpModule.namespace])
    let newQueryList = cloneDeep(queryList)
    newQueryList[0].list = area1
    yield put({ type: 'updateStore', payload: { queryList:newQueryList } });
  },
   //改变查询条件
   *inputChange({ payload }, { call, put, select }) {
    let { queryList,products,center,markersGroup} = yield select(state => state[tmpModule.namespace])
    const { key, value } = payload;
    let newCenter = {}
    let data = queryList.find(item => item.key === key);
    if(data.key == 'region'){
      yield put({type: 'getProducts', payload:{region:value}})
    }else if(data.key == 'productId'){
      var productInfo=products.filter((item,index)=>{
        return item.id == value
      })
      newCenter.longitude = productInfo[0].saleAddressLng || productInfo[0].longitude
      newCenter.latitude = productInfo[0].saleAddressLat || productInfo[0].latitude
    }else if(data.key == 'mapType'){
      markersGroup=[]
      yield put({ type: 'updateStore', payload: { mapType:value,markersGroup,pluginProp:pluginProps } });
    }
    data.value = value;
    //刷新列表参数
    yield put({ type: 'updateStore', payload: { queryList: [...queryList, ...data],center:Object.keys(newCenter).length>0?newCenter:center } });
  },
  *getProducts({payload},{put,call,select}){
    let {biz,products,queryList} = yield select(state => state[tmpModule.namespace])
    const {success,rows} = yield call(projectsServ,{...payload});
    if(success){
      rows.map((item,index)=>{
        item.id = item.productId
      })
      queryList[1].list =rows
      yield put({ type: 'updateStore',payload:{products:rows,queryList}})
    }
  },



  },
  
 

  // 存入状态机
  reducers: {
    updateStore(preState, action) {
      return { ...preState, ...action.payload };
    }
  }
}

export default tmpModule
